package md.lanjing.com.module_home.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.CountDownTimerUtils;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.RegexUtils;
import com.lxh.baselibray.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;


import com.lxh.baselibray.constant.Constant;

import md.lanjing.com.module_home.R;
import md.lanjing.com.module_home.R2;
import md.lanjing.com.module_home.bean.RegisterResponse;
import md.lanjing.com.module_home.bean.RegisterSecurityCodeResponse;
import md.lanjing.com.module_home.contact.RegisterContact;
import retrofit2.Response;


/**
 * 注册
 */
public class RegisterActivity extends MvpActivity<RegisterContact.RegisterPresent> implements RegisterContact.IRegisterView{

    @BindView(R2.id.et_tel)
    EditText etTel;
    @BindView(R2.id.et_security_code)
    EditText etSecurityCode;
    @BindView(R2.id.tv_get_code)
    TextView tvGetCode;
    @BindView(R2.id.et_login_pwd)
    EditText etLoginPwd;
    @BindView(R2.id.et_confirm_login_pwd)
    EditText etConfirmLoginPwd;
    @BindView(R2.id.et_deal_pwd)
    EditText etDealPwd;
    @BindView(R2.id.et_confirm_deal_pwd)
    EditText etConfirmDealPwd;
    @BindView(R2.id.et_invite_code)
    EditText etInviteCode;

    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_register;
    }


    @OnClick({R2.id.tv_get_code, R2.id.btn_register})
    public void onViewClicked(View view) {
        String tel = etTel.getText().toString();
        int i = view.getId();
        if (i == R.id.tv_get_code) {
            if (ObjectUtils.isEmpty(tel)) {
                ToastUtils.showLongToast(context, "手机号不能为空");
                return;
            }
            if (!RegexUtils.isMobileExact(tel)) {
                ToastUtils.showLongToast(context, "请输入正确的手机号");
                return;
            }
            mPresent.getRegisterSecurityCode(context, tel);

        } else if (i == R.id.btn_register) {
            String securityCode = etSecurityCode.getText().toString();
            String logPwd = etLoginPwd.getText().toString();
            String confirmLoginPwd = etConfirmLoginPwd.getText().toString();
            String dealPwd = etDealPwd.getText().toString();
            String confirmDealPwd = etConfirmDealPwd.getText().toString();
            String inviteCode = etInviteCode.getText().toString();

            if (validateRegister(tel, securityCode, logPwd, confirmLoginPwd, inviteCode, dealPwd, confirmDealPwd))
                return;

            showLoadingDialog();
            mPresent.register(context, tel, securityCode, logPwd, inviteCode, dealPwd);

        }
    }

    @Override
    public void getRegisterData(Response<RegisterResponse> response) {
        dismissLoadingDialog();
        if (response.body().getCode() == Constant.SUCCESS_CODE) {
            ToastUtils.showLongToast(context, "注册成功");
            finish();
        } else {
            ToastUtils.showLongToast(context, response.body().getMsg());
        }
    }

    @Override
    public void getRegisterSecurityCodeData(Response<RegisterSecurityCodeResponse> response) {
        if (response.body().getCode() == Constant.SUCCESS_CODE) {
            ToastUtils.showLongToast(context, "已发送验证码");
            CountDownTimerUtils countDownTimerUtils = new CountDownTimerUtils(tvGetCode, 60000, 1000);
            countDownTimerUtils.start();
        } else {
            ToastUtils.showLongToast(context, response.body().getMsg());
        }
    }

    @Override
    protected RegisterContact.RegisterPresent createPresent() {
        return new RegisterContact.RegisterPresent();
    }

    private boolean validateRegister(String tel, String securityCode, String logPwd, String confirmLoginPwd, String inviteCode, String dealPwd, String confirmDealPwd) {
        if (ObjectUtils.isEmpty(tel)) {
            ToastUtils.showLongToast(context, "手机号不能为空");
            return true;
        }
        if (!RegexUtils.isMobileExact(tel)) {
            ToastUtils.showLongToast(context, "请输入正确的手机号");
            return true;
        }
        if (ObjectUtils.isEmpty(securityCode)) {
            ToastUtils.showLongToast(context, "验证码不能为空");
            return true;
        }
        if (ObjectUtils.isEmpty(logPwd)) {
            ToastUtils.showLongToast(context, "登录密码不能为空");
            return true;
        }
        if (ObjectUtils.isEmpty(confirmLoginPwd)) {
            ToastUtils.showLongToast(context, "确认登录密码不能为空");
            return true;
        }
        if (ObjectUtils.isEmpty(dealPwd)) {
            ToastUtils.showLongToast(context, "交易密码不能为空");
            return true;
        }
        if (ObjectUtils.isEmpty(confirmDealPwd)) {
            ToastUtils.showLongToast(context, "确认交易密码不能为空");
            return true;
        }
        if (ObjectUtils.isEmpty(inviteCode)) {
            ToastUtils.showLongToast(context, "邀请码不能为空");
            return true;
        }
        if (!ObjectUtils.equals(logPwd, confirmLoginPwd)) {
            ToastUtils.showLongToast(context, "两次登录密码输入不一致");
            return true;
        }
        if (!RegexUtils.isLoginPwd(logPwd)){
            ToastUtils.showLongToast(context,getString(R.string.login_pwd_regex));
            return true;
        }
        if (!ObjectUtils.equals(dealPwd, confirmDealPwd)) {
            ToastUtils.showLongToast(context, "两次交易密码输入不一致");
            return true;
        }
        return false;
    }
}
